package com.kafka.config;

import com.kafka.utils.IpUtils;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaAdmin;

import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class TopicConfig {




    //创建TopicName为topic.quick.initial的Topic并设置分区数为8以及副本数为1
    //修改后|分区数量会变成11个 注意分区数量只能增加不能减少
    @Bean
    //通过bean创建(bean的名字为initialTopic)
    public NewTopic initialTopic1() {
        return new NewTopic("ChatMsgTest",8, (short) 1 );
    }

    @Bean
    public NewTopic initialTopic2() {
        return new NewTopic("logMessage",8, (short) 1 );
    }

    @Bean
    public NewTopic initialTopic3() {
        return new NewTopic("smsMessage",8, (short) 1 );
    }

    @Bean
    //创建一个kafka管理类，相当于rabbitMQ的管理类rabbitAdmin,没有此bean无法自定义的使用adminClient创建topic
    public KafkaAdmin kafkaAdmin() throws SocketException {
        String IP = IpUtils.getLocalIp4Address().get().toString().replaceAll("/","");
        Map<String, Object> props = new HashMap<>();
        //配置Kafka实例的连接地址 kafka的地址，不是zookeeper
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
        return new KafkaAdmin(props);
    }

    @Bean
    //kafka客户端，在spring中创建这个bean之后可以注入并且创建topic
    public AdminClient adminClient() throws SocketException {
        return AdminClient.create(kafkaAdmin().getConfig());
    }


}